package com.example.controller;


import com.example.domain.message.MessageConstant;
import com.example.domain.result.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user") //底层是通过session实现的
public class UserController {

   //用户登录
    @RequestMapping("/login")
    public String login(@RequestBody com.example.domain.pojo.User user){
        return null;
    }


    //获取登录用户用户的用户名
    @RequestMapping("/getname")
    public Result getname(){
        //认证信息对象 getPrincipal 得到user对象 获取登录信息 ,包括权限等
        Authentication a= SecurityContextHolder.getContext().getAuthentication();

        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        if(user!=null){
            String name=user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,name);
        }
        return new Result(false,MessageConstant.GET_USERNAME_FAIL);
    }

}
